package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.CarriageSelectEntity;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class SubSetPopWindow extends BasePopWindow {

    private TextView okView;
    private TextView resetView;
    private TagFlowLayout tagFlowLayout;
    private LayoutInflater mInflater;
    private List<CarriageSelectEntity> destCity;
    private List<CarriageSelectEntity> returnData;
    private Set<Integer> selectPosSet;

    public SubSetPopWindow(Context context) {
        super(context);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_sub_set;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        tagFlowLayout = holder.getPop().findViewById(R.id.flow_layout1);
        okView = holder.getPop().findViewById(R.id.ok_view);
        resetView = holder.getPop().findViewById(R.id.reset_view);
        returnData = new ArrayList<>();
        okView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                returnData.clear();
                Log.e("select0", selectPosSet.toString());
                if (selectPosSet.size() != 0) {
                    for (int s : selectPosSet) {
                        returnData.add(destCity.get(s));
                    }
                }
                if (onResultClick != null) {
                    onResultClick.result(returnData, selectPosSet, null);
                }
                dissMiss();
            }
        });


        resetView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        tagFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
            @Override
            public void onSelected(Set<Integer> datas) {
                selectPosSet = datas;
            }
        });
    }

    public void setData(List<CarriageSelectEntity> destCity) {
        this.destCity = destCity;
        tagFlowLayout.setAdapter(new TagAdapter<CarriageSelectEntity>(destCity) {
            @Override
            public View getView(FlowLayout parent, int position, CarriageSelectEntity s) {
                TextView tv = (TextView) mInflater.inflate(R.layout.su_set_tag,
                        tagFlowLayout, false);
                tv.setText(s.getName());
//                ColorStateList color = mContext.getResources().getColorStateList(R.color.select_sub_set);
//                tv.setTextColor(color);
                return tv;
            }
        });

    }

    public void setSelect(Set<Integer> selectPosSet) {
        this.selectPosSet = selectPosSet;
        tagFlowLayout.getAdapter().setSelectedList(selectPosSet);

    }
}
